package wqas

import (
	"encoding/json"
	"io"
	"math/rand"
	"net/http"
	"os"
	"strconv"

	"github.com/sirupsen/logrus"
)

type AnalysisHandler struct {
}

func NewAnalysisHandler() *AnalysisHandler {
	return &AnalysisHandler{}
}

func (weld *AnalysisHandler) Sleep() int {

	r := rand.Intn(3)
	return r
}

func (weld *AnalysisHandler) GetUIAnalysis(w http.ResponseWriter, r *http.Request) {
	logrus.Info("GetUIAnalysis ")
	if r.Method == "GET" {
		w.Header().Set("Content-Type", "application/json")

		r.ParseForm()
		page, _ := strconv.Atoi(r.Form.Get("page"))
		pageSize, _ := strconv.Atoi(r.Form.Get("pageSize"))

		reviewDate := r.Form.Get("reviewDate")
		defectType := r.Form.Get("defectType")

		containerNo := r.Form.Get("containerNo")
		weldSeamNo := r.Form.Get("weldSeamNo")
		quickDate := r.Form.Get("quickDate")
		dateBegin := r.Form.Get("dateBegin")
		dateEnd := r.Form.Get("dateEnd")

		logrus.Info("page: ", page, " pageSize: ", pageSize, " reviewDate: ", reviewDate, " defectType: ", defectType, " containerNo: ", containerNo, " weldSeamNo: ", weldSeamNo, " quickDate: ", quickDate, " dateBegin: ", dateBegin, " dateEnd: ", dateEnd)

		inputFile := "webserver/assets/wqas/analysis/getUIAnalysis_en.json"

		inputReader, error := os.Open(inputFile)

		if error != nil {
			println("Error ", error)
			io.WriteString(w, "response is error ")
		} else {
			// buf, err := os.ReadFile(inputFile)

			// inputReader = bufio.NewReader(inputReader)

			// io.WriteString()
			io.Copy(w, inputReader)
			inputReader.Close()
		}
	} else {
		data := make(map[string]interface{})
		data["code"] = 200
		data["body"] = make(map[string]interface{})
		data["msg"] = "the api must be post method "

		jsonByte, _ := json.Marshal(data)
		str := string(jsonByte)
		io.WriteString(w, str)
	}
}

/*
选择焊接参数
*/
/* 选择工艺参数 */
func (weld *AnalysisHandler) GetDefectCause(w http.ResponseWriter, r *http.Request) {
	logrus.Info("GetDefectCause ")
	if r.Method == "GET" {
		w.Header().Set("Content-Type", "application/json")

		r.ParseForm()
		page, _ := strconv.Atoi(r.Form.Get("page"))
		pageSize, _ := strconv.Atoi(r.Form.Get("pageSize"))

		reviewDate := r.Form.Get("reviewDate")
		defectType := r.Form.Get("defectType")

		// containerNo := r.Form.Get("containerNo")
		// weldSeamNo := r.Form.Get("weldSeamNo")
		// quickDate := r.Form.Get("quickDate")
		// dateBegin := r.Form.Get("dateBegin")
		// dateEnd := r.Form.Get("dateEnd")

		logrus.Info("page: ", page, " pageSize: ", pageSize, " reviewDate: ", reviewDate, " defectType: ", defectType)

		inputFile := "webserver/assets/wqas/analysis/getDefectCause_en.json"

		inputReader, error := os.Open(inputFile)

		if error != nil {
			println("Error ", error)
			io.WriteString(w, "response is error ")
		} else {
			// buf, err := os.ReadFile(inputFile)

			// inputReader = bufio.NewReader(inputReader)

			// io.WriteString()
			io.Copy(w, inputReader)
			inputReader.Close()
		}
	} else {
		data := make(map[string]interface{})
		data["code"] = 200
		data["body"] = make(map[string]interface{})
		data["msg"] = "the api must be post method "

		jsonByte, _ := json.Marshal(data)
		str := string(jsonByte)
		io.WriteString(w, str)
	}
}
